package Servlet;

import java.io.IOException;


import java.io.OutputStream;
import java.sql.SQLException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;

import Service.HotelService;
import Utils.AliyunPicture;
import Utils.JsonUtils;
import entity.City;
import entity.Country;
import entity.Hotel;
import entity.Hotel_label;
import entity.Hotel_reserve;
import entity.PageBean;
import entity.Payment;
import entity.ZC_user;

@WebServlet("/hotel")
@MultipartConfig
public class HotelServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	// 加载服务器
	private HotelService service = new HotelService();
	// 加载工具类
	private JsonUtils json = new JsonUtils();
	// 云端图位置
	private String savePath ="gc0910/middleProject/group6/product/";
	
	@Override
	protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		if (method.equals("findCountry")) {
		    // 查询国际list
			findCountry(request, response);
		} else if(method.equals("findCity")) {
			// 查询某个国家下城市list
			findCity(request, response);
		}else if(method.equals("findCountryName")) {
			// 根据国家的id得到单个国家对象（未用）
			findCountryName(request, response);
		}else if(method.equals("findHotelByHotelId")) {
			// 根据hotel的id得到单个Hotel对象（单个）
			findHotelByHotelId(request, response);
		}else if(method.equals("findTypeByHotelId")) {
			// 根据hotel的id得到单个Hotel_Reserve对象（单个）
			findTypeByHotelId(request, response);
		}else if(method.equals("findCountryStore")) {
			// 根据国家id查询该国家下所有的商品
			findCountryStore(request, response);
		}else if(method.equals("findAllStore")) {
			// 查询所有商品（未用）
			findAllStore(request, response);
		}else if(method.equals("findHotelLabel")) {
			// 商品的所有描述
			findHotelLabel(request, response);
		}else if(method.equals("pageQuery")) {
			// 分页展示
			pageQuery(request, response);
		}else if(method.equals("findStoreByCityId")) {
			// 查询单个城市下的所有酒店
			findStoreByCityId(request, response);
		}else if(method.equals("allPageQuery")) {
			// 查询所有商品 但有分页功能
			allPageQuery(request, response);
		}else if(method.equals("getFreshProduct")) {
			// 新鲜甩尾
			getFreshProduct(request, response);
		}else if(method.equals("getHotelByCityName")) {
			// 根据city_name，最终得到所有hotel
			getHotelByCityName(request, response);
		}else if(method.equals("showImage")) {
			showImage(request, response);
		}else if(method.equals("addSeeNumber")) {
			// 增加浏览量
			addSeeNumber(request, response);
		}else if(method.equals("queryOrders")) {
			// 支付成功
			queryOrders(request, response);
		}else if(method.equals("queryZcUsername")) {
			queryZcUsername(request, response);
		}else if(method.equals("queryZcUserid")) {
			queryZcUserid(request, response);
		}
	}
	
	private void queryZcUserid(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		String user_Name = request.getParameter("user_Name");
		ZC_user zUser  = service.queryZcUserid(user_Name);
		System.out.println(zUser);
		json.writeValue(zUser, response);
	}

	private void queryZcUsername(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		String id = request.getParameter("user_id");
		int user_id = Integer.parseInt(id);
		System.out.println("user_id--->"+user_id);
		ZC_user zUser  = service.queryZcUsername(user_id);
		System.out.println(zUser);
		json.writeValue(zUser, response);
	}

	/**
	 * 更改订单状态
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonGenerationException 
	 * @throws SQLException 
	 */
	private void queryOrders(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		// 订单号
		String number = request.getParameter("number");
		System.out.println("number=============="+number);
		int num = service.queryOrders(number);
		if (num !=0) {
			System.out.println("支付状态改变成成功....");
		}
		
		// 查询payment里面的hotel_id
		Payment payment = service.queryPay(number);
		// 得到用户登录的id
		int user_id = payment.getUser_id();
		int hotel_id = payment.getHotel_id();
		System.out.println("查询得到的hotel_id---->"+hotel_id);
		// 更具hotel_id--->hotel表中两个数据改变
		Hotel hotel = service.querHotel(hotel_id);
		System.out.println("servlet_hotel-------------->"+hotel);
		int i = service.updateHotel(hotel.getHotel_id(),hotel.getHotel_sale_number(),hotel.getHotel_save_number());
		if(i!=0) {
			System.out.println("销售量改变成功......");
		}
		json.writeValue(user_id, response);
	}

	private void addSeeNumber(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("增加浏览量方法...");
		String id = request.getParameter("hotel_id");
		int hotel_id = Integer.parseInt(id);
		int num = service.addSeeNumber(hotel_id);
		if(num!=0) {
			System.out.println("修改成功...");
		}
	}

	/**
	 * 根据hotel_id得到hotel_reserve对象
	 */
	private void findTypeByHotelId(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		System.out.println("查询hotel_reserve对象...");
		String id = request.getParameter("hotel_id");
		System.out.println("id--->"+id);
		int hotel_id = Integer.parseInt(id);
		Hotel_reserve type = null;
		try {
			type = service.findTypeByHotelId(hotel_id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		json.writeValue(type, response);
	}

	/**
	 * hotel_id--->Hotel对象
	 */
	private void findHotelByHotelId(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException{
		String id = request.getParameter("hotel_id");
		int hotel_id = Integer.parseInt(id);
		Hotel hotel = service.findHotelByHotelId(hotel_id);
		json.writeValue(hotel, response);
	}
	/**
	 * 根据city_name，最终得到所有hotel
	 * @throws IOException 
	 */
	private void getHotelByCityName(HttpServletRequest request, HttpServletResponse response) throws IOException{
		// 1.采集数据   当前页-----每页显示的数据----city_id
		String city_name = request.getParameter("cityName");
		String currentPageStr = request.getParameter("currentPage");
		String pageSizeStr = request.getParameter("pageSize");
		// 2.数据处理
		int currentPage = 0 ;// 当前页，默认1页
		if (currentPageStr != null && currentPageStr.length() > 0) {
			currentPage = Integer.parseInt(currentPageStr);
		}else {
			currentPage = 1 ;
		}
		
		int pageSize = 0;// 每页显示条数，默认10条数据
		if (pageSizeStr != null && pageSizeStr.length() > 0) {
			pageSize = Integer.parseInt(pageSizeStr);
		}else {
			pageSize = 10 ;
		}
		PageBean<Hotel> hotels = null;
		hotels = service.getCityIdByCityName(city_name, currentPage, pageSize);
		// 存到request域中
//		request.setAttribute("pb",hotels);
//		request.getRequestDispatcher("jsp/lsx/travel_Store_jd.jsp").forward(request, response);
		// 数据返回给前端
		json.writeValue(hotels, response);
	}
	/**
	 * 查询两组数据
	 * 每组4个
	 */
	private void getFreshProduct(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		System.out.println("getFreshProduct方法运行中....");
		String city_ids = request.getParameter("city_id");
		int city_id = Integer.parseInt(city_ids);
		List<Hotel> hotel = service.getFreshProduct(city_id);
		json.writeValue(hotel, response);
	}
	/**
	 * 查询所有商品
	 * @param request
	 * @param response
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	private void allPageQuery(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		// 1.采集数据   当前页-----每页显示的数据----city_id
		String currentPageStr = request.getParameter("currentPage");
		String pageSizeStr = request.getParameter("pageSize");
		// 2.数据处理
		int currentPage = 0 ;// 当前页，默认1页
		if (currentPageStr != null && currentPageStr.length() > 0) {
			currentPage = Integer.parseInt(currentPageStr);
		}else {
			currentPage = 1 ;
		}
		
		int pageSize = 0;// 每页显示条数，默认10条数据
		if (pageSizeStr != null && pageSizeStr.length() > 0) {
			pageSize = Integer.parseInt(pageSizeStr);
		}else {
			pageSize = 10 ;
		}
		// 3.调用service方法
		PageBean<Hotel> pb = service.allPageQuery(currentPage,pageSize);
		// 4.序列化json,给前台
		json.writeValue(pb, response);
	}
	/**
	 * 查询单个城市下的所有酒店
	 * @param request
	 * @param response
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	private void findStoreByCityId(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		// 1.得到city_id
		String id = request.getParameter("city_id");
		int city_id = Integer.parseInt(id);
		// 2.调用服务器的方法
		List<Hotel> lis = service.findStoreByCityId(city_id);
		// 3.响应给前台
		json.writeValue(lis, response);
	}
	/**
	 * 分页功能
	 */
	private void pageQuery(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		// 1.采集数据   当前页-----每页显示的数据----city_id
		String currentPageStr = request.getParameter("currentPage");
		String pageSizeStr = request.getParameter("pageSize");
		String city_idStr = request.getParameter("city_id");
		// 2.数据处理
		int city_id = 0;
		if (city_idStr != null && city_idStr.length() > 0) {
			city_id = Integer.parseInt(city_idStr);
		}else {
			city_id = 1;
		}
		
		int currentPage = 0 ;// 当前页，默认1页
		if (currentPageStr != null && currentPageStr.length() > 0) {
			currentPage = Integer.parseInt(currentPageStr);
		}else {
			currentPage = 1 ;
		}
		
		int pageSize = 0;// 每页显示条数，默认10条数据
		if (pageSizeStr != null && pageSizeStr.length() > 0) {
			pageSize = Integer.parseInt(pageSizeStr);
		}else {
			pageSize = 10 ;
		}
		// 3.调用service方法
		PageBean<Hotel> pb = service.pageQuery(city_id,currentPage,pageSize);
		// 4.序列化json,给前台
		json.writeValue(pb, response);
	}
	/**
	 * 根据前端给的hotel_id
	 * 得到该商品所有的标签
	 * @param request
	 * @param response
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	private void findHotelLabel(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		// 1、得到hotel_id
		String id = request.getParameter("hotel_id");
		int hotel_id = Integer.parseInt(id);
		// 2、调用service方法
		List<Hotel_label> hotel_label = service.findHotelLabel(hotel_id);
		// 3、响应数据
		json.writeValue(hotel_label, response);
	}
	/**
	 * 查询所有商品
	 * @param request
	 * @param response
	 * @throws JsonGenerationException
	 * @throws JsonMappingException
	 * @throws IOException
	 */
	private void findAllStore(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		// 1.调用服务方法，显示所有商品
		List<Hotel> hotel= service.findAllStore();
		// 2、响应给前端
		json.writeValue(hotel, response);
	}
	/**
	 * 暂时没用
	 * 根据国家的id查询城市名称
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonGenerationException 
	 */
	private void findCountryStore(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		// 1.获取城市的id
		// 1.采集数据   当前页-----每页显示的数据----city_id
		String currentPageStr = request.getParameter("currentPage");
		String pageSizeStr = request.getParameter("pageSize");
		String id = request.getParameter("country_id");
		int country_id =0;
		if (id != null && id.length()>0) {
			country_id= Integer.parseInt(id);
		}else {
			// 查询所有的商品
			allPageQuery(request, response);
			return;
		}
		int currentPage = 0 ;// 当前页，默认1页
		if (currentPageStr != null && currentPageStr.length() > 0) {
			currentPage = Integer.parseInt(currentPageStr);
		}else {
			currentPage = 1 ;
		}
		
		int pageSize = 0;// 每页显示条数，默认10条数据
		if (pageSizeStr != null && pageSizeStr.length() > 0) {
			pageSize = Integer.parseInt(pageSizeStr);
		}else {
			pageSize = 10 ;
		}
		// 查询所有的城市id
		List<City> city = service.findCityIDByCountryId(country_id);
		// 返回分页方法
		PageBean<Hotel> pb = service.allPagesByAllCityId(currentPage, pageSize, city);
		// 2.响应给前台
		json.writeValue(pb, response);
	}	
	/**
	 * 根据国家的id查询国家名称
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonGenerationException 
	 */
	private void findCountryName(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		// 1.获取country的id
		String id = request.getParameter("country_id");
		int country_id = Integer.parseInt(id);
		Country country = service.findCountryName(country_id);
		// 2.响应给前台
		json.writeValue(country, response);
	}
	/**
	 * 查找某国家下城市功能
	 * @param request
	 * @param response
	 * @throws IOException 
	 * @throws JsonMappingException 
	 * @throws JsonGenerationException 
	 */
	private void findCity(HttpServletRequest request, HttpServletResponse response) throws JsonGenerationException, JsonMappingException, IOException {
		// 1、调用服务器的方法
		// 获取前端数据
		String id = request.getParameter("country_id");
		int country_id = Integer.parseInt(id);
		List<City> city = service.findCity(country_id);
		// 2、响应前端数据
		json.writeValue(city, response);
		
	}

	/**
	 * 国家查询功能
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public void findCountry (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		// 1、调用服务器方法
		// 1.1判断是否为空
		List<Country> country = service.findCountry();
		// 2、响应给前端
		json.writeValue(country, response);
	}
	
	/**
	 * 显示图片
	 */
	public void showImage(HttpServletRequest request, HttpServletResponse response) {
		try {
			String ticketShowPic = request.getParameter("ticketShowPic");
			ticketShowPic = savePath + ticketShowPic;
			//获取response相应的输出流
			//一个请求request--->相应response
			OutputStream out = response.getOutputStream();
			//将这个图片的字节直接写到前端
			AliyunPicture fileOSSUtils = new AliyunPicture();
			fileOSSUtils.downloadFile(out, ticketShowPic);
			//关闭资源
			out.flush();
			out.close();
			//刷新一下响应的缓存
			response.flushBuffer();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
